require_dependency RAILS_ROOT + '/vendor/plugins/annotations/lib/app/controllers/annotations_controller'

class AnnotationsController < ApplicationController
  before_filter :authorize # overrides :authorize from application_controller
  
  # Override for create method so it doesn't redirect to annotations/new.html.erb
  # when an error occurs
  def create
    if params[:annotation][:source_type].blank? and params[:annotation][:source_id].blank?
      if logged_in?
        params[:annotation][:source_type] = current_user.class.name
        params[:annotation][:source_id] = current_user.id
      end
    end
    
    @annotation = Annotation.new(params[:annotation])
    @annotation.annotatable = @annotatable
    @test = "test"

    respond_to do |format|
      if @annotation.save
        flash[:notice] = 'Annotation was successfully created.'
        format.html { redirect_to :back }
        format.xml  { render :xml => @annotation, :status => :created, :location => @annotation }
      else
        flash[:notice] = 'There was an error whilst trying to create the annotation.'
        format.html { redirect_to :back }
        format.xml  { render :xml => @annotation.errors, :status => :unprocessable_entity }
      end
    end
  end
 
  protected
  
  def authorize
#    user = User.find_by_id(session[:user_id])
#    unless user # NOT LOGGED IN
#      session[:original_uri] = request.request_uri
#      flash[:notice] = "Please log in" 
#      redirect_to :controller => "home", :action => "login"
#    end
  end
end
